﻿namespace Away.IdentityServer.Core.Api;

public static class IdResourceApi
{
    /// <summary>
    /// 身份资源管理接口
    /// </summary>
    /// <param name="endpoint"></param>
    /// <returns></returns>
    public static IEndpointRouteBuilder MapIdResourceApi(this IEndpointRouteBuilder endpoint)
    {
        endpoint.MapGet("/id/resource/list", List);
        endpoint.MapGet("/id/resource", Page);
        endpoint.MapPost("/id/resource", Save);
        endpoint.MapDelete("/id/resource", Remove);
        return endpoint;
    }

    /// <summary>
    /// 查询身份资源列表
    /// </summary>
    /// <returns></returns>    
    private static IResult List(IIdResourceService service)
    {
        var data = service.List();
        return ApiResult.Ok(data);
    }

    /// <summary>
    /// 查询身份资源分页列表
    /// </summary>
    private static IResult Page([AsParameters] IdResourcesSearch search, IIdResourceService service)
    {
        return service.Page(search);
    }

    /// <summary>
    /// 添加、编辑身份资源
    /// </summary>
    private static IResult Save([FromBody] IdentityResource model, IIdResourceService service)
    {
        service.Save(model);
        return ApiResult.Ok();
    }
    /// <summary>
    /// 删除身份资源
    /// </summary>
    private static IResult Remove([FromBody] List<int> ids, IIdResourceService service)
    {
        service.Remove(ids);
        return ApiResult.Ok();
    }
}